package plugins.lxfa.flvModule
{
    import caurina.transitions.*;
    import core.manager.pluginManager.*;
    import core.manager.sceneManager.*;
    import core.manager.scriptManager.*;
    import flash.events.*;
    import mx.containers.*;
    import mx.controls.dataGridClasses.*;
    import mx.core.*;
    import util.view.player.*;
    import util.view.player.event.*;
    import view.loadings.*;

    public class FlvBase extends EventDispatcher
    {
        private var container:Canvas;
        private var onPlayCompleteScript:String;
        private var flvPlayer:FLVPlayer;
        private var loading_mc:LoadingWaveRota;

        public function FlvBase()
        {
            container = PluginManager.getInstance().getFloor(2);
            ScriptManager.getInstance().addApi(ScriptName.SHOW_FLV, initPlayer);
            ScriptManager.getInstance().addApi(ScriptName.REMOVE_FLV, dispose);
            ScriptManager.getInstance().addApi(ScriptName.INIT_ON_FLV_PLAY_COMPLETE_SCRIPT, initOnPlayCompleteScript);
        }// end function

        private function on_complete(DataGridListData:FLVPlayerEvent) : void
        {
            if (loading_mc != null)
            {
                if (loading_mc.parent != null)
                {
                    loading_mc.parent.removeChild(loading_mc);
                }
            }
            loading_mc = null;
            flvPlayer.resume();
            return;
        }// end function

        private function on_play_complete(DataGridListData:NetStatusEvent) : void
        {
            ScriptManager.getInstance().runScriptDirectly(onPlayCompleteScript);
            return;
        }// end function

        private function initOnPlayCompleteScript(DataGridListData:String) : void
        {
            this.onPlayCompleteScript = DataGridListData;
            return;
        }// end function

        private function onClose(DataGridListData:Event) : void
        {
            this.dispatchEvent(new Event(Event.CLOSE));
            flvPlayer.removeEventListener(Event.CLOSE, onClose);
            flvPlayer.removeEventListener(NetStatusEvent.NET_STATUS, on_play_complete);
            return;
        }// end function

        private function PROGRESS_refresh(DataGridListData:ProgressEvent) : void
        {
            loading_mc.updateByProgressEvent(DataGridListData);
            return;
        }// end function

        public function initPlayer(url:String, DataGridListData:Boolean = false) : void
        {
            flvPlayer = new FLVPlayer(url, 900, 480, DataGridListData);
            container.addChild(flvPlayer);
            loading_mc = new LoadingWaveRota();
            loading_mc.x = Application.application.width / 2;
            loading_mc.y = Application.application.height / 2;
            Application.application.addChildAt(loading_mc, Application.application.numChildren-1);
            flvPlayer.y = 70;
            flvPlayer.addEventListener(NetStatusEvent.NET_STATUS, on_play_complete);
            flvPlayer.addEventListener(Event.CLOSE, onClose);
            flvPlayer.addEventListener(ProgressEvent.PROGRESS, PROGRESS_refresh);
            flvPlayer.addEventListener(FLVPlayerEvent.COMPLETE, on_complete);
            SceneManager.getInstance().addEventListener(SceneChangeEvent.COMPLETE, dispose);
            return;
        }// end function

        public function dispose(DataGridListData:SceneChangeEvent) : void
        {
            var e:* = DataGridListData;
            if (flvPlayer != null)
            {
                flvPlayer.pause();
                flvPlayer.parent.removeChild(flvPlayer);
                flvPlayer.dispose();
                flvPlayer = null;
            }
            return;
        }// end function

    }
}
